package com.znz.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: johnson
 * Date: 5/7/15
 * Time: 3:39 下午
 */
@RooJavaBean
@RooToString
@RooJpaActiveRecord(versionField = "", identifierColumn = "id", identifierField = "id", table = "t_order")
public class Torder {


	//	订单号,系统内唯一
	@Column(name = "order_no")
	private String orederNo;
	//	客户姓名
	@Column(name = "user_name")
	private String userName;
    //	客户年龄
	@Column(name = "user_age")
	private int userAge;
    @Column(name = "user_phone")
    //	客户电话
    private String userPhone;
    @Column(name = "user_city")
	//	用户所在城市
	private int userCity;
	//	用户留言
	@Column(name = "user_message")
	private String userMessage;
	//	客户性别
	@Column(name = "user_sex")
	private int userSex;
	//	订单备注
	@Column(name = "note")
	private String note;
	//	创建订单客服ID
    @ManyToOne
    @JoinColumn(name="c_user_id")
	private Tcustomer tcustomer;
	//	状态1初始化2已派单3已接单4完单
	@Column(name = "state")
	private Integer state;
	//	接单回访状态0 未回访 1 已回访
	@Column(name = "return_visit_receive")
	private Integer returnVisitReceive;
	//	结单回访状态 0 未回访 1 已回访
	@Column(name = "return_visit_success")
	private Integer returnVisitSuccess;
	//	是否可用,是否删除0可用1删除
	@Column(name = "enabled")
	private Integer enabled;
	//	创建时间
	@Column(name = "create_date")
	private Date createDate;
	//	修改时间
	@Column(name = "update_date")
	private Date updateDate;

    @OneToMany(mappedBy = "torder",cascade= CascadeType.ALL)
    @JsonIgnore
    private List<TorderHospitalProject> torderHospitalProjectList;
    /**
     * 方法说明:获取部位所包含的项目
     * @param where
     * @param params
     * @return
     * @throws Exception
     */
    public static List<Project> getProjectAllByPartId(String where,List<Object> params) throws Exception{
        StringBuilder str_query =  new StringBuilder("SELECT o FROM Project o where o.status!=-1 ");
        str_query.append(where);
        str_query.append(" order by o.id ");
        if(params!=null){
            TypedQuery<Project> q = entityManager().createQuery(
                    str_query.toString(), Project.class);
            int i=1;
            for(Object key :params){
                q.setParameter(i,key);
                i++;
            }
            return q.getResultList();
        }
        return null;
    }

    /**
     * 方法说明:获取订单信息(客服)
     * @param where
     * @param params
     * @param firstResult
     * @param maxResults
     * @return
     */
    public static  List<Torder> getTorderAllByWhere(String where,List<Object> params,int firstResult,int maxResults){
        StringBuilder strQuery=new StringBuilder();
        strQuery.append("SELECT * FROM t_order WHERE enabled=1 ");
        strQuery.append(where);
        strQuery.append(" order by id");
        Query query=entityManager().createNativeQuery(strQuery.toString(),Torder.class).setFirstResult(firstResult).setMaxResults(maxResults);
        if(null!=params && params.size()>0){
            for(Object key:params){
                query.setParameter(1,key);
            }
            return query.getResultList();
        }
        return  query.getResultList();
    }
    public static  List<Torder> getTorderAllByWheretest(String where,List<Object> params){
        StringBuilder strQuery=new StringBuilder();
        strQuery.append("SELECT * FROM t_order WHERE enabled=1 ");
        strQuery.append(where);
        strQuery.append(" order by id");
        Query query=entityManager().createNativeQuery(strQuery.toString(),Torder.class);
        if(null!=params && params.size()>0){
            for(Object key:params){
                query.setParameter(1,key);
            }
            return query.getResultList();
        }
        return  query.getResultList();
    }

    /**
     * 方法说明:获取订单列表(医院查看)
     * @param where
     * @param params
     * @param firstResult
     * @param maxResults
     * @return
     * @throws Exception
     */
    public static List<Torder> getTorderAllByHospital(String where,List<Object> params,int firstResult,int maxResults)throws Exception{
        StringBuilder strQ_query=new StringBuilder("SELECT o FROM Torder o where o.enabled=1 ");
        strQ_query.append(where);
        strQ_query.append(" order by o.id");
        TypedQuery<Torder> query=entityManager().createQuery(strQ_query.toString(),Torder.class).setFirstResult(firstResult).setMaxResults(maxResults);
        if(null!=params && params.size()>0){
            int i=1;
            for (Object key:params){
                query.setParameter(i,"%"+key+"%");
                i++;
            }
            return  query.getResultList();
        }
        return query.getResultList();
    }
    /**
     * 方法说明:获取订单分配所有的医院(获取订单所分配的项目)
     * @param orderId  订单Id
     * @return
     */
    public static List<TorderHospitalProject> getProjectListbyOrderId(Long orderId){
        StringBuilder sbSql=new StringBuilder();
        sbSql.append("SELECT o FROM TorderHospitalProject o where o.torder.id=?");
        TypedQuery<TorderHospitalProject> query=entityManager().createQuery(sbSql.toString(),TorderHospitalProject.class);
        query.setParameter(1,orderId);
        return query.getResultList();
    }
}
